
/**************************************************************************************************
Filename:       NBSleep_Demo.c
Editor:         Tome @ newbit
Revised:        $Date: 2017/2/21 11:20:02 +0800  $
Revision:       $Revision: 00001 $

Description:    
History:        
Notes:          share on github.com/newbitstudio
support on bbs.newbitstudio.com

**************************************************************************************************/



/**************************************************************************************************
// INCLUDES
**************************************************************************************************/
#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"
#include "OSAL_PwrMgr.h"


#if !defined( WIN32 )
#include "OnBoard.h"
#endif

#include "ul_strings.h"

/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_uart.h"


#include "NBSleep_Demo.h"


/**************************************************************************************************
// TYPEDEF
**************************************************************************************************/



/**************************************************************************************************
// CONSTANTS
**************************************************************************************************/

#define TASK_ID_SELF NBSleep_Demo_TaskID



// This list should be filled with Application specific Cluster IDs.
const cId_t NewbitDemo_ClusterList[NEWBITDEMO_MAX_CLUSTERS] =
{
  NEWBITDEMO_CLUSTERID_REPORT
};

const SimpleDescriptionFormat_t NewbitDemo_SimpleDesc =
{
  NEWBITDEMO_ENDPOINT,              //  int Endpoint;
  NEWBITDEMO_PROFID,                //  uint16 AppProfId[2];
  NEWBITDEMO_DEVICEID,              //  uint16 AppDeviceId[2];
  NEWBITDEMO_DEVICE_VERSION,        //  int   AppDevVer:4;
  NEWBITDEMO_FLAGS,                 //  int   AppFlags:4;
  NEWBITDEMO_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  (cId_t *)NewbitDemo_ClusterList,  //  byte *pAppInClusterList;
  NEWBITDEMO_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  (cId_t *)NewbitDemo_ClusterList   //  byte *pAppInClusterList;
};

// This is the Endpoint/Interface description.  It is defined here, but
// filled-in in NewbitDemo_Init().  Another way to go would be to fill
// in the structure here and make it a "const" (in code space).  The
// way it's defined in this sample app it is define in RAM.
endPointDesc_t NewbitDemo_epDesc;


/**************************************************************************************************
// LOCAL VERIABLE
**************************************************************************************************/
byte NBSleep_Demo_TaskID;   // Task ID for internal task/event processing


/**************************************************************************************************
// FUNCTIONS DECLERATION
**************************************************************************************************/


static void NBSleep_Demo_HandleKeys( uint8 shift, uint8 keys );

void NBSleep_Demo_EnterLowpower(void);
void NBSleep_Demo_ExitLowpower(void);


/**************************************************************************************************
// FUNCTIONS
**************************************************************************************************/

/*********************************************************************
* @fn      NBSleep_Demo_Init
*
* @brief   Initialization function for the Generic App Task.
*          This is called during initialization and should contain
*          any application specific initialization (ie. hardware
*          initialization/setup, table initialization, power up
*          notificaiton ... ).
*
* @param   task_id - the ID assigned by OSAL.  This ID should be
*                    used to send messages and set timers.
*
* @return  none
*/
void NBSleep_Demo_Init( uint8 task_id )
{
  TASK_ID_SELF = task_id;
  
  
  // Fill out the endpoint description.
  NewbitDemo_epDesc.endPoint = NEWBITDEMO_ENDPOINT;
  NewbitDemo_epDesc.task_id = &TASK_ID_SELF;
  NewbitDemo_epDesc.simpleDesc
    = (SimpleDescriptionFormat_t *)&NewbitDemo_SimpleDesc;
  NewbitDemo_epDesc.latencyReq = noLatencyReqs;
  
  // Register the endpoint description with the AF
  afRegister( &NewbitDemo_epDesc );
  
  
  // Register for all key events - This app will handle all key events
  RegisterForKeys( TASK_ID_SELF );
  
  
    // Update the display
#if defined ( LCD_SUPPORTED )
  HalLcdWriteString( "NewBit Demo", HAL_LCD_LINE_1 );

  HalLcdWriteString( "newbit.com.cn", HAL_LCD_LINE_4 );
#endif  
  
  
  
}




/*********************************************************************
* @fn      NBSleep_Demo_ProcessEvent
*
* @brief   Generic Application Task event processor.  This function
*          is called to process all events for the task.  Events
*          include timers, messages and any other user defined events.
*
* @param   task_id  - The OSAL assigned task ID.
* @param   events - events to process.  This is a bit map and can
*                   contain more than one event.
*
* @return  none
*/
uint16 NBSleep_Demo_ProcessEvent( uint8 task_id, uint16 events )
{
  afIncomingMSGPacket_t *MSGpkt;
  
  
  if ( events & SYS_EVENT_MSG )
  {
    MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( NBSleep_Demo_TaskID );
    while ( MSGpkt )
    {
      switch ( MSGpkt->hdr.event )
      {
      case ZDO_CB_MSG:
        //NBSleep_Demo_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
        break;
        
      case KEY_CHANGE:
        NBSleep_Demo_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
        break;
        
      case AF_DATA_CONFIRM_CMD:
        
        break;
        
      case AF_INCOMING_MSG_CMD:
        //NBSleep_Demo_MessageMSGCB( MSGpkt );
        break;
        
      case ZDO_STATE_CHANGE:
        //NBSleep_Demo_NwkState = (devStates_t)(MSGpkt->hdr.status);
        
        /*
        if ( ( NBSleep_Demo_NwkState == DEV_ROUTER )          
        || ( NBSleep_Demo_NwkState == DEV_END_DEVICE )  )
        osal_start_reload_timer(TASK_ID_SELF, NBD_EVT_DHT11, 1000 ); 
        */
        
        break;
        
      case ONEBUS_EVENT:
        
        break;
        
      default:
        break;
      }
      // Release the memory
      osal_msg_deallocate( (uint8 *)MSGpkt );
      
      // Next
      MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( TASK_ID_SELF );
    }
    // return unprocessed events
    return (events ^ SYS_EVENT_MSG);
  }
  
  
  
  if ( events & NB_SLEEP_EVT )
  {
    NBSleep_Demo_EnterLowpower();
    osal_pwrmgr_device(PWRMGR_BATTERY);    
    
    ZDApp_StopJoiningCycle();
    
    return ( events ^ NB_SLEEP_EVT );
  }
  
  if ( events & NB_WAKE_EVT )
  {
    NBSleep_Demo_ExitLowpower();
    osal_pwrmgr_device(PWRMGR_ALWAYS_ON);    
    
    ZDApp_StartJoiningCycle();
    
    return ( events ^ NB_WAKE_EVT );
  }  
  
  
  return 0;
}


/**************************************************************************************************
* @fn      NBSleep_Demo_EnterLowpower
*
* @brief   
*
* @param   void
*
* @return  void
**************************************************************************************************/
void NBSleep_Demo_EnterLowpower(void)
{
  
  P0SEL = 0X00;
  P0DIR = 0x00;
  
  P1SEL = 0x00;
  P1DIR = 0X00;
  
  P2DIR &= ~0X07;
  
}


/**************************************************************************************************
* @fn      NBSleep_Demo_ExitLowpower
*
* @brief   
*
* @param   void
*
* @return  void
**************************************************************************************************/
void NBSleep_Demo_ExitLowpower(void)
{
  
}






/*********************************************************************
* @fn      NBSleep_Demo_HandleKeys
*
* @brief   Handles all key events for this device.
*
* @param   shift - true if in shift/alt.
* @param   keys - bit field for key events. Valid entries:
*                 HAL_KEY_SW_4
*                 HAL_KEY_SW_3
*                 HAL_KEY_SW_2
*                 HAL_KEY_SW_1
*
* @return  none
*/
static void NBSleep_Demo_HandleKeys( uint8 shift, uint8 keys )
{
  char keystr[] = "SN ";
  
  // Shift is used to make each button/switch dual purpose.
  if ( shift )
  {
    if ( keys & HAL_KEY_SW_1 )
    {      
    }
    if ( keys & HAL_KEY_SW_2 )
    {
    }
    if ( keys & HAL_KEY_SW_3 )
    {
    }
    if ( keys & HAL_KEY_SW_4 )
    {
    }
  }
  else
  {
    if ( keys & HAL_KEY_SW_0 )
    {
      HalLcdWriteString("SLEEPING", HAL_LCD_LINE_2);
      osal_set_event( TASK_ID_SELF, NB_SLEEP_EVT);           
      keystr[1] = '0';
    }
    
    if ( keys & HAL_KEY_SW_1 )
    {
      //choujiang();
      
      HalLcdWriteString("SLEEPING", HAL_LCD_LINE_2);
      osal_set_event( TASK_ID_SELF, NB_SLEEP_EVT);      
      keystr[1] = '1';
    }
    
    if ( keys & HAL_KEY_SW_2 )
    {
      HalLcdWriteString("WAKE", HAL_LCD_LINE_2);
      osal_set_event( TASK_ID_SELF, NB_WAKE_EVT);        
      
      keystr[1] = '2';
    }
    
    if ( keys & HAL_KEY_SW_3 )
    {
      keystr[1] = '3';
    }
    
    if ( keys & HAL_KEY_SW_4 )
    {
      keystr[1] = '4';
    }
    
    if ( keys & HAL_KEY_SW_5 )
    {
      keystr[1] = '5';
    }
    HalLcdWriteString(keystr, HAL_LCD_LINE_4);
  }
}

/**************************************************************************************************
Copyright 2016-2017 Newbit Studio. All rights reserved.
**************************************************************************************************/



